package cn.gov.cus.rmftquartz.controller;

import cn.gov.cus.rmftquartz.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 维护任务的对外接口类(支持跨域)
 *
 * @author kyou on 2017/12/30 下午1:02
 */
@Slf4j
@CrossOrigin(origins = "*",maxAge = 3600)
@RestController
@RequestMapping("/quartz")
public class QuartzController {
    private final Scheduler scheduler;

    @Autowired
    public QuartzController(Scheduler scheduler){
        this.scheduler = scheduler;
    }

    /**
     * 暂停任务
     *
     * @param jobName  任务名
     * @param jobGroup 任务所在分组
     * @throws Exception Exception
     */
    @PostMapping(value = "/pauseJob")
    public Result pauseJob(@RequestParam(value = "jobName") String jobName,
                           @RequestParam(value = "jobGroup") String jobGroup){
        Result result = new Result(true,"任务已暂停");
        try {
            scheduler.pauseJob(JobKey.jobKey(jobName, jobGroup));
        }catch (SchedulerException e){
            result.setSuccess(false);
            result.setMsg("暂停任务失败！");
            log.debug(e.getMessage());
        }
        return result;
    }

    /**
     * 恢复任务
     *
     * @param jobName  任务名
     * @param jobGroup 任务所属分组
     * @throws Exception Exception
     */
    @PostMapping(value = "/resumeJob")
    public Result resumeJob(@RequestParam(value = "jobName") String jobName,
                          @RequestParam(value = "jobGroup") String jobGroup) throws Exception {
        Result result = new Result(true,"任务已恢复");
        try {
            scheduler.resumeJob(JobKey.jobKey(jobName, jobGroup));
        }catch (SchedulerException e){
            result.setSuccess(false);
            result.setMsg("恢复任务失败！");
            log.debug(e.getMessage());
        }
        return result;
    }

    /**
     * 重新调度任务
     *
     * @param jobName        任务名
     * @param jobGroup       任务分组分组
     * @param cronExpression cron 表达式
     * @throws Exception Exception
     */
    @PostMapping(value = "/rescheduleJob")
    public Result rescheduleJob(@RequestParam(value = "jobName") String jobName,
                              @RequestParam(value = "jobGroup") String jobGroup,
                              @RequestParam(value = "cronExpression") String cronExpression){
        Result result = new Result(true,"修改成功！");
        try {
            TriggerKey triggerKey = TriggerKey.triggerKey(jobName+"Trigger", jobGroup);

            // 表达式调度构建器
            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);

            CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);

            // 按新的cronExpression表达式重新构建trigger
            trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();

            // 按新的trigger重新设置job执行
            scheduler.rescheduleJob(triggerKey, trigger);
        } catch (SchedulerException e) {
            result.setSuccess(false);
            result.setMsg("修改任务失败！");
            log.debug("更新定时任务失败" + e.getMessage());
        }
        return result;
    }

    /**
     * 删除任务
     *
     * @param jobName  任务名
     * @param jobGroup 任务所在分组
     * @throws Exception Exception
     */
    @PostMapping(value = "/deleteJob")
    public Result deleteJob(@RequestParam(value = "jobName") String jobName,
                          @RequestParam(value = "jobGroup") String jobGroup) throws Exception {
        Result result = new Result(true,"删除成功！");
        try {
            scheduler.pauseTrigger(TriggerKey.triggerKey(jobName, jobGroup));
            scheduler.unscheduleJob(TriggerKey.triggerKey(jobName, jobGroup));
            scheduler.deleteJob(JobKey.jobKey(jobName, jobGroup));
        } catch (SchedulerException e) {
            result.setSuccess(false);
            result.setMsg("删除任务失败！");
            log.debug(e.getMessage());
        }
        return result;
    }

    /*@GetMapping(value = "/queryJob")
    public Map<String, Object> queryJob(@RequestParam(value = "pageNum") Integer pageNum,
                                        @RequestParam(value = "pageSize") Integer pageSize) {
        PageInfo<JobAndTrigger> jobAndTrigger = iJobAndTriggerService.getJobAndTriggerDetails(pageNum, pageSize);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("JobAndTrigger", jobAndTrigger);
        map.put("number", jobAndTrigger.getTotal());
        return map;
    }*/
}
